/**
 * Created by oneminute on 7/17/14.
 */

var lz = require("../lib/utils/modules")();

exports.post = function (_req, _res, _opts, _callback) {
    var user = _req.user;
    var description = _req.body.description;
    if (!user) {
        lz.Logger.warn("/releaseneed: 用户为临时用户。");
        _callback(lz.Config.errors.getError(lz.Config.errors.REQUEST_TEMPUSER_ERROR));
        return;
    }
    lz.Logger.debug("/releaseneed: 用户请求发布新需求。", description);
    var demand = new lz.Demand({
        id: lz.Utils.getUUID("d_"),
        description: description,
        uid: user.id
    });
    demand.save(function (_err, _demand) {
        lz.Logger.debug("更新用户对象的上架商品列表: ", _demand);
        user.demands.unshift(demand);
        user.save({saveAll: false}, function (_err, _user) {
            lz.Logger.debug("/releaseneed: 发布需求完成。");
            if (_err) {
                _callback(_err);
            } else {
                _callback(null, user);
            }
        });
    });
};